from http.server import BaseHTTPRequestHandler, HTTPServer
import os

PORT = 8080
LINK_FILE = "tunnel_url.txt"  # 保存在 Flask 项目根目录

class LinkHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header("Content-type", "text/html; charset=utf-8")
        self.end_headers()
        if os.path.exists(LINK_FILE):
            with open(LINK_FILE, "r") as f:
                url = f.read().strip()
        else:
            url = "尚未获取到公网地址"
        self.wfile.write(f"<h1>当前公网地址：</h1><p><a href='{url}'>{url}</a></p>".encode())

if __name__ == "__main__":
    print(f"广播服务器已启动：端口 {PORT}")
    server = HTTPServer(('', PORT), LinkHandler)
    server.serve_forever()
